package com.zj.leetcode;

public class A27移除元素 {

    public static void main(String[] args) {
        int[] nums={0,1,2,2,3,0,4,2};
        int val=2;
        System.out.println(removeElement(nums,val));
    }


    public static  int removeElement(int[] nums, int val) {
        int temp,length=0;
        for (int i = 0,j=nums.length-1; i <=j;i++) {
            while(i < nums.length&&nums[i]!=val){
                i++;
            }
            if(i <= j){
                length++;
            }
            while(i<j&&nums[j]==val){
                j--;
                length++;
            }
            if(i<j){
                temp=nums[i];
                nums[i]=nums[j];
                nums[j]=temp;
                j--;
            }
        }

        return nums.length-length;

    }
}
